import pandas as pd
from apyori import apriori  # 使用 apyori 库进行关联规则挖掘

# 创建数据
data = [
    [True, True, True, True, False, False, True, True, False, False],
    [True, True, True, True, True, True, False, True, False, False],
    [False, True, True, True, False, False, False, True, False, True],
    [False, True, False, False, True, False, True, True, False, True],
    [True, True, False, True, True, False, True, True, False, True],
    [True, False, True, False, False, True, True, True, False, False],
    [False, True, False, True, True, False, True, True, False, True],
    [True, False, True, True, True, False, True, True, True, False],
    [False, True, True, True, True, False, False, True, False, False],
    [True, False, True, False, True, True, False, True, False, True],
    [False, False, True, False, True, False, False, True, True, True],
    [True, False, False, True, True, True, False, True, False, True],
    [False, True, True, False, True, True, False, True, False, True],
    [True, True, True, False, False, True, False, True, False, False],
    [True, True, False, False, True, True, False, True, False, False]
]

# 转换为DataFrame
df = pd.DataFrame(data, columns=[
    'Arbori_binari_de_cautare', 'Arbori_optimali', 'Arbori_echilibrati_in_inaltime',
    'Arbori_Splay', 'Arbori_rosu_negru', 'Arbori_2_3', 'Arbori_B', 'Arbori_TRIE',
    'Sortare_topologica', 'Algoritmul_Dijkstra'
])

# 将布尔型数据转换为适合apriori算法的格式
# apyori 库的输入数据必须是一个事务列表，每个事务包含一个项目集
transactions = df.apply(lambda row: [col for col, val in row.items() if val], axis=1).tolist()

# 运行apriori算法，找出频繁项集
frequent_itemsets = apriori(transactions, min_support=0.6, min_confidence=0.6, min_lift=1.0)

# 显示关联规则
print("关联规则挖掘结果如下：")
for index, item in enumerate(frequent_itemsets, start=1):
    # 处理频繁项集部分的打印
    frequent_set = {elem for elem in item.items}
    print(f"{index}. 频繁项集:")
    print(f"    - {{{', '.join(frequent_set)}}}")

    # 处理关联规则部分的打印
    for rule in item.ordered_statistics:
        antecedent = {elem for elem in rule.items_base}
        consequent = {elem for elem in rule.items_add}
        support = item.support
        confidence = rule.confidence
        lift = rule.lift
        print(f"{index}. 关联规则:")
        print(f"    - {{{', '.join(antecedent)}}} -> {{{', '.join(consequent)}}} (support: {support}, confidence: {confidence}, lift: {lift})")
    print("-" * 50)